package cn.livingCloud.controller.core.validator;

import cn.livingCloud.dto.EmployeeBindMemberDto;
import cn.livingCloud.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

/**
 * Created by 唐旭 on 2016/6/1.
 */
@Component
public class EmployeeBindMemberDtoValidator implements Validator {

    @Autowired
    private EmployeeService employeeService;

    @Override
    public boolean supports(Class<?> aClass) {
        return EmployeeBindMemberDto.class.equals(aClass);
    }

    @Override
    public void validate(Object target, Errors errors) {
        EmployeeBindMemberDto formDto = (EmployeeBindMemberDto) target;
        validateMember(formDto, errors);
    }

    private void validateMember(EmployeeBindMemberDto formDto, Errors errors) {
        String memberUuid = formDto.getMemberUuid();
        if (StringUtils.isEmpty(memberUuid)) {
            errors.rejectValue("memberUuid", null, "请输入昵称选择相应人员绑定");
            return;
        }

        boolean bound = employeeService.checkMemberBound(memberUuid);
        if (bound) {
            errors.rejectValue("memberUuid", null, "该人员已被绑定");
        }
    }
}
